/*
 * traininfo_transfer.c
 *
 *  Created on: 2013-2-14
 *      Author: panqiuhui
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include "../head/communicate_with_server/traininfo_transfer.h"

extern int socketfd_traininfo;

//输出车站信息
void output_stationinfo(StationItem item) {
	printf("-----------------------------------------------\n");
	printf("stationName: %s\n", item.stationName);
	printf("stationArriveTime :%s\n", item.stationArriveTime);
	printf("stationDepartTime :%s\n", item.stationDepartTime);
	printf("stationTimelast: %s\n", item.consumeTime);
	printf("stationPrice: %s\n", item.price);
	printf("-----------------------------------------------\n");
}

//向服务器发送查询车站起售时间的请求并等待回复，查询到结果返回true，否则返回false，结果在station中
bool query_saletime(struct stationinfo *station) {
	bool result = true;
	struct traininfo_request_type type;
	type.type = TYPE_QUERY_SQLETIME;
	int write_return = write(socketfd_traininfo, &type, sizeof(type));
	if (write_return == -1) {
		perror("traininfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}
	write_return = write(socketfd_traininfo, station, sizeof(*station)); //发送仅包含车站名的stationinfo
	if (write_return == -1) {
		perror("traininfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	int read_return = read(socketfd_traininfo, &result, sizeof(result));
	if (read_return == -1) {
		perror("traininfo_transfer.c: read error!\n");
		exit(EXIT_FAILURE);
	}

	if (result) {
		memset(station, 0, sizeof(*station));
		int read_return = read(socketfd_traininfo, station, sizeof(*station));
		if (read_return == -1) {
			perror("traininfo_transfer.c: read error!\n");
			exit(EXIT_FAILURE);
		}
	}

	return result;
}

//向服务器发送查询列车经过的车站信息的请求并等待回复，查询到结果返回true，否则返回false，结果在stationlist列表中
bool search_train_number(const struct train_number_request *request,
		StationList *stationlist) {
	bool result = true;
	struct traininfo_request_type type;
	type.type = TYPE_SEARCH_TRAIN_NUM;
	int write_return = write(socketfd_traininfo, &type, sizeof(type));
	if (write_return == -1) {
		perror("traininfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	write_return = write(socketfd_traininfo, request, sizeof(*request)); //发送请求
	if (write_return == -1) {
		perror("traininfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	int read_return = read(socketfd_traininfo, &result, sizeof(result));
	if (read_return == -1) {
		perror("traininfo_transfer.c: read error!\n");
		exit(EXIT_FAILURE);
	}

	if (result) {
		bool islast = false;
		while (!islast) {
			StationItem item;
			memset(&item, 0, sizeof(item));
			read_return = read(socketfd_traininfo, &item, sizeof(item));
			if (read_return == -1) {
				perror("traininfo_transfer.c: read error!\n");
				break;
			}
			if (!AddItemStation(item, stationlist)) {
				break;
			}
			read_return = read(socketfd_traininfo, &islast, sizeof(islast));
			if (read_return == -1) {
				perror("traininfo_transfer.c: read error!\n");
				break;
			}
		}
	}

	return result;
}

//向服务器发送查询发到站信息的请求并等待回复，查询到结果返回true，否则返回false，结果在start_end_stationlist列表中
bool search_start_end_station(
		const struct train_start_end_station_request *request,
		StartEndStationList *start_end_stationlist) {
	bool result = true;
	struct traininfo_request_type type;
	type.type = TYPE_SEARCH_START_END_STATION;
	int write_return = write(socketfd_traininfo, &type, sizeof(type));
	if (write_return == -1) {
		perror("traininfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	write_return = write(socketfd_traininfo, request, sizeof(*request)); //发送请求
	if (write_return == -1) {
		perror("traininfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	int read_return = read(socketfd_traininfo, &result, sizeof(result));
	if (read_return == -1) {
		perror("traininfo_transfer.c: read error!\n");
		exit(EXIT_FAILURE);
	}

	if (result) {
		bool islast = false;
		while (!islast) {
			StartEndStationItem item;
			memset(&item, 0, sizeof(item));
			read_return = read(socketfd_traininfo, &item, sizeof(item));
			if (read_return == -1) {
				perror("traininfo_transfer.c: read error!\n");
				break;
			}
			if (!AddItemStartEndStation(item, start_end_stationlist)) {
				break;
			}
			read_return = read(socketfd_traininfo, &islast, sizeof(islast));
			if (read_return == -1) {
				perror("traininfo_transfer.c: read error!\n");
				break;
			}
		}
	}

	return result;
}

//向服务器发送查询车站经过车次的请求并等待回复，查询到结果返回true，否则返回false，结果在station_pass_train_list列表中
bool search_station_pass_train(const struct station_pass_train_request *request,
		StationPassTrainList *station_pass_train_list) {
	bool result = true;
	struct traininfo_request_type type;
	type.type = TYPE_SEARCH_STATION_PASS_TRAIN;
	int write_return = write(socketfd_traininfo, &type, sizeof(type));
	if (write_return == -1) {
		perror("traininfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	write_return = write(socketfd_traininfo, request, sizeof(*request)); //发送请求
	if (write_return == -1) {
		perror("traininfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	int read_return = read(socketfd_traininfo, &result, sizeof(result));
	if (read_return == -1) {
		perror("traininfo_transfer.c: read error!\n");
		exit(EXIT_FAILURE);
	}

	if (result) {
		bool islast = false;
		while (!islast) {
			StationPassTrainItem item;
			memset(&item, 0, sizeof(item));
			read_return = read(socketfd_traininfo, &item, sizeof(item));
			if (read_return == -1) {
				perror("traininfo_transfer.c: read error!\n");
				break;
			}
			if (!AddItemStationPassTrain(item, station_pass_train_list)) {
				break;
			}
			read_return = read(socketfd_traininfo, &islast, sizeof(islast));
			if (read_return == -1) {
				perror("traininfo_transfer.c: read error!\n");
				break;
			}
		}
	}

	return result;
}

//向服务器发送余票查询的请求并等待回复，查询到结果返回true，否则返回false，结果在ticketlist列表中
bool search_ticket_left(const struct ticket_left_request *request,
		TicketList *ticketlist) {
	bool result = true;
	struct traininfo_request_type type;
	type.type = TYPE_SEARCH_TICKET_LEFT;
	int write_return = write(socketfd_traininfo, &type, sizeof(type));
	if (write_return == -1) {
		perror("traininfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	write_return = write(socketfd_traininfo, request, sizeof(*request)); //发送请求
	if (write_return == -1) {
		perror("traininfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	int read_return = read(socketfd_traininfo, &result, sizeof(result));
	if (read_return == -1) {
		perror("traininfo_transfer.c: read error!\n");
		exit(EXIT_FAILURE);
	}

	if (result) {
		bool islast = false;
		while (!islast) {
			TicketItem item;
			memset(&item, 0, sizeof(item));
			read_return = read(socketfd_traininfo, &item, sizeof(item));
			if (read_return == -1) {
				perror("traininfo_transfer.c: read error!\n");
				break;
			}
			if (!AddItemTicket(item, ticketlist)) {
				break;
			}
			read_return = read(socketfd_traininfo, &islast, sizeof(islast));
			if (read_return == -1) {
				perror("traininfo_transfer.c: read error!\n");
				break;
			}
		}
	}

	return result;
}

//向服务器发送购票请求并等待回复，查询到结果返回true，否则返回false，车票信息存储在ticket中
bool buy_ticket(const struct buy_ticket_request*request,
		struct ticket_saled_info *ticket) {
	bool result = true;
	struct traininfo_request_type type;
	type.type = TYPE_BUY_TICKET;
	int write_return = write(socketfd_traininfo, &type, sizeof(type));
	if (write_return == -1) {
		perror("traininfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	write_return = write(socketfd_traininfo, request, sizeof(*request)); //发送请求
	if (write_return == -1) {
		perror("traininfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	int read_return = read(socketfd_traininfo, &result, sizeof(result));
	if (read_return == -1) {
		perror("traininfo_transfer.c: read error!\n");
		exit(EXIT_FAILURE);
	}

	if (result) {
		read_return = read(socketfd_traininfo, ticket, sizeof(*ticket));
		if (read_return == -1) {
			perror("traininfo_transfer.c: read error!\n");
			exit(EXIT_FAILURE);
		}
	}
	return result;
}
